home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT20.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  11.5 KB  |  432 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 20                         
  5.                                           
  6.  This program uses the same sprites as last time but                        
  7.  this time in a shoot-em-up game.                                           
  8.                                           
  9.  *** PROJECT ***                                                             
  10.  This program requires the file WGT5_WC.LIB to be linked.                    
  11.                                           
  12.  *** DATA FILES ***                                                          
  13.  Make sure that SPACE.SPR is in your executable directory.                   
  14.                                WATCOM C++ VERSION 
  15. ==============================================================================
  16. */
  17.  
  18. #include <wgt5.h>
  19.  
  20. /* Large example demonstrating many functions from WGT  */
  21.  
  22. #define MAXEXPLOS 20
  23. #define MAXALIEN 70
  24.  
  25.  
  26. block workscreen, backgroundscreen;
  27. block sprites[21];
  28.  
  29. typedef struct {
  30.      short x, y;
  31.      short dirx, diry;
  32.      short shootx, shooty;
  33.      short power;
  34.     } spaceship;
  35.  
  36. typedef struct {
  37.     short x, y;
  38.     short num;
  39.     } explosion;
  40. explosion explos[MAXEXPLOS];
  41.  
  42. spaceship aliens[70];
  43. short numaliens, aliensleft;
  44.  
  45. short oldmode;
  46. short shoot[10], sx[10], sy[10];
  47. short ship;                        /* Sprite to show */
  48. short shipx[50], shipy[50];        /* Ship coordinates */
  49. float incx, incy, yourx, youry;
  50.  
  51. short i, j, k;                    /* Loop control */
  52. short scrolloffset = 1;           /* A counter for the scroll offset */
  53. short ix = 0, iy = 0, 
  54.       ix2 = 319, iy2 = 199;       /* coords of scrolling area */
  55. short speed;                      /* the scrolling speed */
  56.                   /* try changing speed and sign */
  57. short nextlevel;                  /* Performing level change = 1 */
  58. short leveltextx;                 /* X coordinate of next level text */
  59. short level;
  60.  
  61. color palette[256];
  62.  
  63.  
  64. void show_opening_screen (void)
  65. /* Displays a title screen */
  66. {
  67. short colordraw;
  68. int i;  
  69.   
  70.   for (i = 0; i < 256; i++)
  71.     wsetrgb (i, 0, 0, 0, palette);      /* Make a black palette */
  72.   
  73.   wsetpalette (0, 255, palette);
  74.   
  75.   wnormscreen ();
  76.   wtextcolor (1);
  77.   wtextbackground (0);
  78.  
  79.   colordraw = 2;
  80.   /* draw a pattern on the screen */
  81.   for (i = 0; i < 320; i++) 
  82.     { 
  83.      colordraw++;
  84.      if (colordraw > 105)
  85.      colordraw = 2;
  86.     
  87.      wsetcolor (colordraw);  
  88.      wfline (160, 100, i, 0);  
  89.     }
  90.   
  91.   for (i = 0; i < 200; i++) 
  92.     { 
  93.      colordraw++;
  94.      if (colordraw > 105)
  95.      colordraw = 2;
  96.     
  97.      wsetcolor (colordraw);  
  98.      wfline (160, 100, 319, i);
  99.     }
  100.   
  101.   for (i = 319; i >= 0; i--) 
  102.     { 
  103.      colordraw++;
  104.      if (colordraw > 105)
  105.      colordraw = 2;
  106.     
  107.      wsetcolor (colordraw);  
  108.      wfline (160, 100, i, 199);
  109.     }
  110.   
  111.   for (i = 199; i >= 0; i--) 
  112.     { 
  113.      colordraw++;
  114.      if (colordraw > 105)
  115.      colordraw = 2;
  116.     
  117.      wsetcolor (colordraw);  
  118.      wfline (160, 100, 0, i);  
  119.     }
  120.  
  121.   
  122.   /* Set up the initial palette */
  123.   wsetrgb (0, 0, 0, 0, palette);
  124.   for (i = 2; i < 54; i++) 
  125.     wsetrgb (i, i, 0, 0, palette);
  126.   for (i = 54; i < 106; i++) 
  127.     wsetrgb (i, 105 - i, 0, 0, palette);
  128.   wsetrgb (253, 60, 60, 60, palette);
  129.   wsetrgb (254, 45, 45, 45, palette);
  130.   wsetrgb (255, 30, 30, 30, palette);
  131.   wsetrgb (1, 63, 63, 63, palette);
  132.  
  133.   wouttextxy (50, 20, NULL, "Shoot 'Em Up sprite example");
  134.   wouttextxy (50, 28, NULL, "showing what YOU could do with");
  135.   wouttextxy (50, 36, NULL, "the WordUp Graphics Toolkit!");
  136.   wouttextxy (50, 44, NULL, "Use the mouse to move");
  137.   wouttextxy (50, 52, NULL, "Left button shoots");
  138.   wouttextxy (50, 60, NULL, "Right button quits");
  139.   wouttextxy (50, 68, NULL, "Press any key to start");
  140.   wsetrgb (1, 63, 63, 63, palette);
  141.   wfade_in (0, 255, 1, palette);
  142.   
  143.   do 
  144.    {
  145.     wcolrotate (2, 106, 0, palette);    /* Rotate the red background */
  146.     wsetpalette (2, 106, palette);
  147.     wretrace ();
  148.    } while (!kbhit ());
  149.   
  150.   while (kbhit ()) getch ();            /* Clear the keyboard buffer */
  151.   wcls (0);
  152. }
  153.  
  154.  
  155.  
  156. void initialize_aliens (short numaliens)
  157. /* Sets the initial positions of the aliens when starting a level. */
  158. {
  159. short i;
  160.  
  161.   for (i = 0; i < numaliens; i++)
  162.   {
  163.     aliens[i].x = rand () % 200 + 30;
  164.     aliens[i].y = rand () % 50;
  165.     aliens[i].dirx = rand () % 8 - 4;
  166.     aliens[i].diry = rand () % 8 - 4;
  167.     aliens[i].power = numaliens / 3;
  168.     aliens[i].shootx = -1;
  169.   }
  170. }
  171.  
  172.  
  173. void make_starfield (void)
  174. /* Gets two virtual screens and puts the star sprites on them to make
  175.    a starfield background. */
  176. {
  177. short i;
  178.  
  179.   workscreen = wnewblock (0, 0, 319, 199);      /* get two virtual screens */
  180.   backgroundscreen = wnewblock (0, 0, 319, 199);
  181.   wsetscreen (backgroundscreen);                /* go to second one */
  182.   wcls (0);                                     /* clear it */
  183.  
  184.   for (i = 1; i < 300; i++)
  185.     wputblock (rand() % 320, rand() % 200, sprites[5], 0);
  186.   /* sprite[5] is small stars */
  187.  
  188.   for (i = 1; i < 5; i++)
  189.     wputblock (rand () % 320, rand () % 200, sprites[6], 0);
  190.   /* sprite[6] is a large star */
  191.   wnormscreen ();
  192. }
  193.  
  194.  
  195. void scrolldown(void)
  196. {
  197. /* With a bit of work, you could make this routine scroll in
  198.    four directions and make new graphics come on as it scrolls!
  199.    Of course, we've already gone and done that for you!
  200.    Check out wscr_wc.lib... */
  201.  
  202.   wcopyscreen (ix, scrolloffset, ix2, iy2, backgroundscreen, 
  203.            ix, iy, workscreen);
  204.   wcopyscreen (ix, iy, ix2, scrolloffset, backgroundscreen, 
  205.            ix, iy2 - scrolloffset, workscreen); 
  206.   
  207.   scrolloffset += speed;                /* Adjust the scrolling offset */
  208.   if ((scrolloffset < iy) && (speed < 0))
  209.     scrolloffset = iy2 + 1 - abs (scrolloffset);
  210.   if ((scrolloffset > iy2) && (speed > 0))
  211.     scrolloffset = abs (iy2 - scrolloffset);
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. void play (void)
  220. {
  221.   scrolldown ();                         
  222.   /* Scroll the background screen by copying it to a different offset on
  223.      workscreen.  This also erases the old frame. */
  224.   
  225.   wsetscreen (workscreen);
  226.   
  227.   if (mouse.but == 1)                   /* if you clicked the mouse */
  228.     {                                   
  229.      for (i = 0; i < 9; i++)
  230.       if (shoot[i] == 0)                /* check to see if slot available */
  231.     {
  232.      shoot[i] = 1;                  /* make it unavailable */
  233.      sx[i] = yourx + 9;             /* set coords for shot */
  234.      sy[i] = youry - 7;
  235.      break;
  236.     }
  237.     }
  238.  
  239.   for (i = 0; i < 9; i++)                         /* if shot is active */
  240.     {
  241.      if (shoot[i] == 1)                           /* then show the sprite */
  242.        {
  243.     wputblock (sx[i], sy[i], sprites[4], 0);  /* at the right coords */
  244.     sy[i] -= 8;                               /* make it go up */
  245.     if (sy[i] < 1)                            /* if it is at top, */
  246.         shoot[i] = 0;                         /* make it available again */
  247.  
  248.     /* Now check to see if it hit an alien by seeing if their
  249.        rectangular regions overlap. */
  250.     for (j = 0; j < numaliens; j++)
  251.       if ((aliens[j].x != -255) &&
  252.        (sx[i] > aliens[j].x) && (sx[i] < aliens[j].x+25) &&
  253.        (sy[i] > aliens[j].y) && (sy[i] < aliens[j].y+20))
  254.          {
  255.           shoot[i] = 0;                       /* Turn off that missile */
  256.           aliens[j].power--;
  257.           if (aliens[j].power == 0)
  258.         {
  259.          aliensleft--;
  260.          if (aliensleft == 0)
  261.            {
  262.             nextlevel = 1;
  263.             leveltextx = ix2 + 100;
  264.            }
  265.          for (k = 0; k < MAXEXPLOS; k++)  /* Add an explosion */
  266.             if (explos[k].num == 0)
  267.               {
  268.                explos[k].x = aliens[j].x;
  269.                explos[k].y = aliens[j].y;
  270.                explos[k].num = 8;
  271.                break;
  272.               }
  273.         
  274.          aliens[j].x = -255;
  275.         }
  276.          }
  277.        }
  278.     }
  279.  
  280.   incx=(float)(mouse.mx - yourx) / 5.0;  /* Move your spaceship according to the */
  281.   incy=(float)(mouse.my - youry) / 5.0;  /* position of the mouse. */
  282.   yourx += incx;
  283.   youry += incy;
  284.  
  285.   if (incx < 2 && incx > -2) 
  286.     ship = 2;
  287.   else if (incx > 5) 
  288.     ship = 3;
  289.   else if (incx < 5) 
  290.     ship = 1;
  291.   wputblock (yourx, youry, sprites[ship], 1); /* Put your ship on */
  292.  
  293.  
  294.   for (i = 0; i < numaliens; i++)
  295.     {
  296.      if (aliens[i].x != -255) /* Alien is still alive */
  297.        {
  298.     aliens[i].x += aliens[i].dirx; /* Make them move */
  299.     aliens[i].y += aliens[i].diry;
  300.  
  301.     /* Make them bounce off the walls if they go too far in one direction. */
  302.     if (aliens[i].x <= ix) 
  303.       aliens[i].dirx = rand () % 4;
  304.     else if (aliens[i].x > ix2 - 20) 
  305.       aliens[i].dirx = -(rand () % 4);
  306.       
  307.     if (aliens[i].y < 0) 
  308.       aliens[i].diry = rand () % 4;
  309.     else if (aliens[i].y > 150) 
  310.       aliens[i].diry = -(rand () % 4);
  311.  
  312.     if ((aliens[i].shootx == -1) && (rand () % 10 == 1))  /* 1 in 10 chance of shooting */
  313.       {
  314.        aliens[i].shootx = aliens[i].x + 9;
  315.        aliens[i].shooty = aliens[i].y + 5;
  316.       }
  317.  
  318.     if (aliens[i].power > 0)
  319.       wputblock (aliens[i].x, aliens[i].y, sprites[7], 1);
  320.  
  321.     /* Display the alien's missile. */
  322.     if (aliens[i].shootx != -1)
  323.       {
  324.        wputblock (aliens[i].shootx, aliens[i].shooty, sprites[8], 1);
  325.        aliens[i].shooty += 4;
  326.        if (aliens[i].shooty > 199) 
  327.          aliens[i].shootx = -1;
  328.       }
  329.        }
  330.     }
  331.  
  332.   /* Now draw any explosions on the screen. */
  333.   for (i = 0; i < MAXEXPLOS; i++)
  334.     if (explos[i].num > 0)
  335.       {
  336.        explos[i].num++;
  337.        if (explos[i].num > 14)
  338.        explos[i].num = 0;
  339.        else wputblock (explos[i].x, explos[i].y, sprites[explos[i].num], 1);
  340.       }
  341.  
  342.  
  343.   if (nextlevel == 1)
  344.   {
  345.     leveltextx -= 10;
  346.     wgtprintf (leveltextx, 50, NULL, "LEVEL %i", level + 1);
  347.  
  348.     if (leveltextx < -100)
  349.       {
  350.        level++;
  351.        if (level > MAXALIEN) 
  352.      level = MAXALIEN;
  353.        numaliens = level + 5;
  354.        aliensleft = numaliens;
  355.        initialize_aliens (numaliens);
  356.        nextlevel = 0;
  357.       }
  358.   }
  359.  
  360.   wretrace ();
  361.   wretrace ();
  362.   wcopyscreen (ix, iy, ix2, iy2, workscreen, ix, iy, NULL);    
  363.   /* copy the first screen to the visual screen */
  364. }
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371. void main (void)
  372. {
  373.   if ( !vgadetected () )
  374.     {
  375.      printf("Error - VGA card required for any WGT program.\n");
  376.      exit (0);
  377.     }
  378.   
  379.   printf ("WGT Example #20\n\n");
  380.   printf ("This is a small game which demonstrates the use of sprites and virtual\n");
  381.   printf ("screens. The player cannot be hurt in this game, so it's not very exciting...\n");
  382.   printf ("\n\nPress any key to continue.\n");
  383.   getch ();
  384.  
  385.   oldmode = wgetmode ();
  386.   vga256 ();
  387.  
  388.   minit ();             
  389.   show_opening_screen ();
  390.   
  391.   wloadsprites (palette, "space.spr", sprites, 0, 20);   /* load sprites */
  392.   wsetpalette (0, 255, palette);
  393.  
  394.   make_starfield ();
  395.  
  396.   wnormscreen ();                                /* go back to normal screen */
  397.   wcls (0);                                      /* clear it */
  398.   wbutt (0, 0, 29, 199);                         /* make some side panels */
  399.   wbutt (252, 0, 319, 199);
  400.   wtextbackground (0);
  401.   wtextcolor (1);
  402.   wtexttransparent (TEXTFG);
  403.  
  404.   yourx = 128;
  405.   youry = 170;
  406.  
  407.   wsetscreen (workscreen);                       /* go to first screen */
  408.   noclick ();
  409.   msetbounds (ix, 150, ix2 - 20, 179);
  410.  
  411.   speed = -4;
  412.  
  413.   nextlevel = 0;
  414.   level = 1;
  415.   numaliens = level + 5;
  416.   aliensleft = numaliens;
  417.  
  418.   initialize_aliens (numaliens);
  419.  
  420.   do {
  421.     play ();
  422.   } while (mouse.but != 2);                    /* until right button is pressed */
  423.   
  424.   mdeinit ();                   /* Deinitialize the mouse handler */
  425.   wfreeblock (workscreen);
  426.   wfreeblock (backgroundscreen);
  427.   wfreesprites (sprites, 0, 20);
  428.   wsetmode (oldmode);
  429. }
  430.  
  431.  
  432.